package com.atguigu.tingshu.user.strategy.impl;

import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.strategy.PaymentStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author atguigu-mqx
 * @ClassName VipPaymentStrategy
 * @description: TODO
 * @date 2024年06月26日
 * @version: 1.0
 */
@Service("1003")
public class VipPaymentStrategy implements PaymentStrategy {

    @Autowired
    private UserVipServiceMapper userVipServiceMapper;

    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void processPayment(UserPaidRecordVo userPaidRecordVo) {
//  购买vip -- user_vip_service
        //  根据订单编号查询数据
        Long count = userVipServiceMapper.selectCount(new LambdaQueryWrapper<UserVipService>().eq(UserVipService::getOrderNo, userPaidRecordVo.getOrderNo()));
        if (count != 0) return;
        //  查询用户购买vip时间;
        Long vipServiceConfigId = userPaidRecordVo.getItemIdList().get(0);
        //  根据Id查询vip服务配置对象
        VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(vipServiceConfigId);
        Integer serviceMonth = vipServiceConfig.getServiceMonth();
        //  创建对象
        UserVipService userVipService = new UserVipService();
        //  赋值：
        userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
        userVipService.setUserId(userPaidRecordVo.getUserId());
        userVipService.setStartTime(new Date());
        //  过期时间：
        //  第一次购买当前时间+购买月份！  第二次购买可能会发生--续费： 当前过期时间+续费时间！
        //  如何判断是否续费：是否vip and vip_expire > 当前系统时间
        UserInfo userInfo = userInfoMapper.selectById(userPaidRecordVo.getUserId());
        //  判断
        if (null != userInfo && userInfo.getIsVip() == 1 && userInfo.getVipExpireTime().after(new Date())){
            //  userInfo.getVipExpireTime() + userInfo.getServiceMonth();
            LocalDateTime localDate = new LocalDateTime(userInfo.getVipExpireTime());
            Date expireTime = localDate.plusMonths(serviceMonth).toDate();
            userVipService.setExpireTime(expireTime);
        } else {
            //  不是续费情况.
            userVipService.setExpireTime(new LocalDate().plusMonths(serviceMonth).toDate());
        }
        //  int i = 1/0;
        //  保存数据
        userVipServiceMapper.insert(userVipService);
        //  普通用户变为vip 。user_info.is_vip = 1;
        userInfo.setIsVip(1);
        userInfo.setVipExpireTime(userVipService.getExpireTime());
        userInfoMapper.updateById(userInfo);
    }
}
